package com.thinkin.services.ordercenter;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkin.allDo.SysDao;
import com.thinkin.ordercenter.util.OchannelUtil;
import com.thinkin.ordercenter.vo.OchannelVo;
import com.thinkin.util.ExtendDate;
import com.thinkin.util.JsonHelper;

public class Ochannel {

	private Logger log = LoggerFactory.getLogger(this.getClass().getName());

	public String add(String jsonstr) {

		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		com.thinkin.ordercenter.po.Ochannel ochannel = new com.thinkin.ordercenter.po.Ochannel();
		String result = "";
		try {
			ochannel.setKeyid(UUID.randomUUID().toString());
			ochannel.setUsername(jsonObject.get("username").toString());
			ochannel.setPwd(jsonObject.get("pwd").toString());
			ochannel.setDsname(jsonObject.get("dsname").toString());
			ochannel.setDstype(jsonObject.get("dstype").toString());
			ochannel.setDsshortcode(jsonObject.get("dsshortcode").toString());
			ochannel.setDscode(jsonObject.get("dscode").toString());
			ochannel.setAuthbrand(jsonObject.get("authbrand").toString());
			ochannel.setProductcategory(jsonObject.get("productcategory")
					.toString());
			if (StringUtils.isNotBlank(jsonObject.get("bail").toString())) {
				ochannel.setBail(Double.parseDouble(jsonObject.get("bail")
						.toString()));
			}

			ochannel.setDbusinessuser(jsonObject.get("dbusinessuser")
					.toString());
			ochannel.setDbusinessmodel(jsonObject.get("dbusinessmodel")
					.toString());
			ochannel.setCreatename(jsonObject.get("createname").toString());
			ochannel.setStrsCreatedate(ExtendDate.getYMD_h_m_s(new Date()));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}

		result = SysDao.getOchannelDaoBuz().add(ochannel);

		return "{\"result\":\"" + result + "\"}";
	}

	public String modify(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		com.thinkin.ordercenter.po.Ochannel ochannel = new com.thinkin.ordercenter.po.Ochannel();
		String result = "";
		try {
			ochannel.setKeyid(jsonObject.get("keyid").toString());
			ochannel.setUsername(jsonObject.get("username").toString());
			ochannel.setPwd(jsonObject.get("pwd").toString());
			ochannel.setDsname(jsonObject.get("dsname").toString());
			ochannel.setDstype(jsonObject.get("dstype").toString());
			ochannel.setDsshortcode(jsonObject.get("dsshortcode").toString());
			ochannel.setDscode(jsonObject.get("dscode").toString());
			ochannel.setAuthbrand(jsonObject.get("authbrand").toString());
			ochannel.setProductcategory(jsonObject.get("productcategory")
					.toString());
			ochannel.setBail(Double.parseDouble(jsonObject.get("bail")
					.toString()));
			ochannel.setDbusinessuser(jsonObject.get("dbusinessuser")
					.toString());
			ochannel.setDbusinessmodel(jsonObject.get("dbusinessmodel")
					.toString());

			ochannel.setModifyname(jsonObject.get("modifyname").toString());
			ochannel.setStrsModifydate(ExtendDate.getYMD_h_m_s(new Date()));

		} catch (JSONException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
			log.error(e.getMessage(), e);
		}

		result = SysDao.getOchannelDaoBuz().modify(ochannel);
		return "{\"result\":\"" + result + "\"}";
	}

	public String findQuery(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		String result = "";
		OchannelVo qvo = new OchannelVo();
		try {
			qvo.setDsName(jsonObject.get("dsname").toString());
			qvo.setAuthBrand(jsonObject.get("authBrand").toString());
			qvo.setDsType(jsonObject.get("dsType").toString());
			qvo.setDbusinessUser(jsonObject.get("dbusinessUser").toString());
			qvo.setDbusinessModel(jsonObject.get("dbusinessModel").toString());
			qvo.setProductCategory(jsonObject.get("productCategorys")
					.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block7
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}

		List<OchannelUtil> utils = SysDao.getOchannelDaoBuz().findQuery(qvo);
		if (utils != null && utils.size() > 0) {
			result = JsonHelper.toJSONString(utils);
		} else {
			result = "{}";
		}
		return result;
	}

	public String loadDataForModify(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		String keyid = "";
		String result = "";
		try {
			keyid = jsonObject.get("keyid").toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		com.thinkin.ordercenter.po.Ochannel ochannel = SysDao
				.getOchannelDaoBuz().loadOchanel(keyid);
		result = JsonHelper.toJSONString(ochannel);

		return result;
	}

	/**
	 * 
	 * @param keyId
	 * @param enableed
	 * @return
	 */
	public String isEnable(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		String result = "";
		try {
			result = SysDao.getOchannelDaoBuz().isEnable(
					jsonObject.get("keyId").toString(),
					jsonObject.get("enableed").toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}

		return "{\"result\":\"" + result + "\"}";
	}

	public String bathBrand(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		String result = "";
		try {
			result = SysDao.getOchannelDaoBuz().bathBrand(
					jsonObject.get("keyIds").toString(),
					jsonObject.get("bandCodes").toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}

		return "{\"result\":\"" + result + "\"}";
	}

	public String delete(String jsonstr) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		String result = "";

		try {
			result = SysDao.getOchannelDaoBuz().delete(
					jsonObject.get("keyId").toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}

		return "{\"result\":\"" + result + "\"}";
	}

}
